/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.controller;

import ac.pbp.core.exception.FailActionType;
import ac.pbp.core.exception.ServiceException;
import ac.pbp.dto.GenreDto;
import ac.pbp.dto.filter.GenreFilterCriteria;
import ac.pbp.model.Genre;
import ac.pbp.service.GenreService;
import ac.pbp.core.util.ViewHelper;
import ac.pbp.dto.GenreDto;
import ac.pbp.model.Genre;
import ac.pbp.service.GenreService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Kimy
 */
@Controller
public class GenreController {

    
    @Autowired
    private GenreService genreService;
    
    @RequestMapping("/genreView.html")
    public ModelAndView viewGenre(HttpServletRequest request) {
        
        String page = request.getParameter("page");
        String mode = request.getParameter("mode");

        if ((page == null) || page.equals("")) {
            page = "1";
        }

        GenreFilterCriteria filter = new GenreFilterCriteria();
        filter.setPage(Integer.valueOf(page));
        filter.setMax(10);
        
        String dataSearch = null;
        if (mode != null) {
            if (mode.equals("search")) {
                dataSearch = request.getParameter("dataSearch");
                if (!dataSearch.equals("")) {
                    filter.setDataSearch(dataSearch);
                }
            }
        }

        GenreDto genreDto = genreService.filterGenre(filter);
        
        ModelAndView model = new ModelAndView("genreView");
        model.addObject("data", genreDto);
        model.addObject("dataSearch", dataSearch);
        
        return model;
    }
    
    @RequestMapping("/genrePrepareAdd.html")
    public ModelAndView prepareAddGenre(HttpServletRequest request) {
        
        ModelAndView model = new ModelAndView("genreAdd");
        
        model.addObject("genreList", genreService.loadAll());
        
        return model;
    }
    
    @RequestMapping(value = "/genreAdd.html", method = RequestMethod.POST)
    public ModelAndView addGenre(@ModelAttribute("genre") Genre genre, HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        
        genreService.addGenre(genre);
        
        return ViewHelper.redirectTo("genreView.html");
    }
    
    @RequestMapping("/genrePrepareUpdate.html")
    public ModelAndView prepareUpdateGenre(HttpServletRequest request) {
        
        String kodeGenre = request.getParameter("kodeGenre");
        
        ModelAndView model = new ModelAndView("genreAdd");
        Genre genre = genreService.loadGenre(kodeGenre);
        model.addObject("genre",genre);
        model.addObject("genreList", genreService.loadAll());
        
        return model;
    }
    
    @RequestMapping(value = "/genreUpdate.html", method = RequestMethod.POST)
    public ModelAndView updateGenre(@ModelAttribute("genre")  Genre genre, HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        
        genreService.updateGenre(genre);
        
        return ViewHelper.redirectTo("genreView.html");
    }
    
    @RequestMapping("/genreDelete.html")
    public ModelAndView deleteGenre(HttpServletRequest request) {
        String idArray = request.getParameter("idArray");
        genreService.deleteGenre(idArray);
        
        return ViewHelper.redirectTo("genreView.html");
    }
}
